package cn.faster.sys.controller;

import cn.faster.sys.entity.SysMenu;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import java.util.*;
import cn.faster.common.result.ResultBean;
import cn.faster.sys.service.SysMenuService;

/**
 * <p>
 * 用户权限表 前端控制器
 * </p>
 *
 * @author xiaoCoder
 * @date 2021-06-10
 */
@RestController
@CrossOrigin
@AllArgsConstructor
@Api(value = "用户权限表",tags = "用户权限表")
@RequestMapping("/sys/menu")
public class SysMenuController {

    private final SysMenuService sysMenuService;

    @ApiOperation(value = "获取Menu根据id",notes="")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @GetMapping("/getById")
    public ResultBean<?> menu(@RequestParam(value = "id") Long id) throws Exception {

        SysMenu sysMenu = sysMenuService.getMenuById(id);
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.fillData(sysMenu);
        return resultBean;
    }

    @ApiOperation(value = "获取Menu列表",notes="")
    @ApiImplicitParam(name = "map", value = "Menu实体中的参数", dataType ="Map")
    @GetMapping("")
    @RequiresPermissions("sys:menu:select")
    public ResultBean<?> menuList() throws Exception {

        return ResultBean.SUCCESS(sysMenuService.listWithTree());
    }


    @ApiOperation(value = "添加Menu",notes="新增一条Menu")
    @ApiImplicitParam(name = "Menu", value = "Menu实体中的参数", required = true, dataType = "Menu")
    @PostMapping("")
    public ResultBean<?> menuAdd(@RequestBody SysMenu sysMenu) throws Exception {

        Integer flag = sysMenuService.addMenu(sysMenu);
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.fillData(flag);
        return resultBean;
    }

    @ApiOperation(value = "修改Menu",notes="根据id修改Menu")
    @ApiImplicitParam(name = "Menu", value = "Menu实体", required = true, dataType = "Menu")
    @PutMapping("")
    public ResultBean<?> menuUpdate(@RequestBody SysMenu sysMenu) throws Exception {

        Integer flag = sysMenuService.updateMenu(sysMenu);
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.fillData(flag);
        return resultBean;
    }
    @ApiOperation(value = "物理删除Menu",notes="根据id物理删除Menu")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @DeleteMapping("/{id}")
    public ResultBean<?> menuDelete(@PathVariable(name = "id") Integer id) throws Exception {

        Integer flag = sysMenuService.deleteMenuById(id);
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.fillData(flag);
        return resultBean;
    }

    @ApiOperation(value = "逻辑删除Menu",notes="根据id逻辑删除Menu")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")
    @PostMapping("/removeById")
    public ResultBean<?> menuRemove(@RequestParam(value = "id") Integer id) throws Exception {

        Integer flag = sysMenuService.removeMenuById(id);
        ResultBean<?> resultBean = new ResultBean<>();
        resultBean.fillData(flag);
        return resultBean;
    }

    @GetMapping("/getAllMenus")
    public ResultBean<?> testMenu() throws Exception {

        return ResultBean.SUCCESS(sysMenuService.listWithTree());
    }

}
